package com.ruoyi.model.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.DbInfo;
import com.ruoyi.model.service.IDbInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据库连接基本信息Controller
 * 
 * @author genghz
 * @date 2021-10-20
 */
@RestController
@RequestMapping("/db/base")
public class DbInfoController extends BaseController
{
    @Autowired
    private IDbInfoService dbInfoService;

    /**
     * 查询数据库连接基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('db:base:list')")
    @GetMapping("/list")
    public TableDataInfo list(DbInfo dbInfo)
    {
        startPage();
        List<DbInfo> list = dbInfoService.selectDbInfoList(dbInfo);
        return getDataTable(list);
    }

    @GetMapping("/all")
    public AjaxResult allDbs(DbInfo dbInfo)
    {
        List<DbInfo> list = dbInfoService.selectDbInfoList(dbInfo);
        return AjaxResult.success(list);
    }

    /**
     * 导出数据库连接基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('db:base:export')")
    @Log(title = "数据库连接基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DbInfo dbInfo)
    {
        List<DbInfo> list = dbInfoService.selectDbInfoList(dbInfo);
        ExcelUtil<DbInfo> util = new ExcelUtil<DbInfo>(DbInfo.class);
        return util.exportExcel(list, "数据库连接基本信息数据");
    }

    /**
     * 获取数据库连接基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('db:base:query')")
    @GetMapping(value = "/{dbId}")
    public AjaxResult getInfo(@PathVariable("dbId") Long dbId)
    {
        return AjaxResult.success(dbInfoService.selectDbInfoByDbId(dbId));
    }

    /**
     * 新增数据库连接基本信息
     */
    @PreAuthorize("@ss.hasPermi('db:base:add')")
    @Log(title = "数据库连接基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DbInfo dbInfo)
    {
        return toAjax(dbInfoService.insertDbInfo(dbInfo));
    }

    /**
     * 修改数据库连接基本信息
     */
    @PreAuthorize("@ss.hasPermi('db:base:edit')")
    @Log(title = "数据库连接基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DbInfo dbInfo)
    {
        return toAjax(dbInfoService.updateDbInfo(dbInfo));
    }

    /**
     * 删除数据库连接基本信息
     */
    @PreAuthorize("@ss.hasPermi('db:base:remove')")
    @Log(title = "数据库连接基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dbIds}")
    public AjaxResult remove(@PathVariable Long[] dbIds)
    {
        return toAjax(dbInfoService.deleteDbInfoByDbIds(dbIds));
    }
}
